# Contributor: Anjandev Momi <anjan@momi.ca>
# Contributor: Maarten van Gompel <proycon@anaproy.nl>
# Maintainer: Willow Barraco <contact@willowbarraco.fr>
pkgname=sxmo-utils
pkgver=1.16.3
pkgrel=0
pkgdesc="Utility scripts, programs, and configs that hold the Sxmo UI environment together"
url="https://git.sr.ht/~mil/sxmo-utils"
arch="all !ppc64le" # limited by codemadness-frontends
license="AGPL-3.0-only"
depends="
	coreutils
	gawk
	curl
	doas
	jq
	"
makedepends="
	libx11-dev
	linux-headers
	scdoc
	xproto
	libcap-utils
	"
options="!check setcap" # has no tests
subpackages="
	$pkgname-doc
	$pkgname-common::noarch
	$pkgname-x11::noarch
	$pkgname-wayland::noarch
	$pkgname-dwm::noarch
	$pkgname-sway::noarch
	$pkgname-audio-pipewire:audio_pipewire:noarch
	$pkgname-audio-pulseaudio:audio_pulseaudio:noarch
	$pkgname-audio-bluetooth:audio_bluetooth:noarch
	$pkgname-audio-bluetooth-pipewire:audio_bluetooth_pipewire:noarch
	$pkgname-audio-bluetooth-pulseaudio:audio_bluetooth_pulseaudio:noarch
	"
source="
	$pkgname-$pkgver.tar.gz::https://git.sr.ht/~mil/sxmo-utils/archive/$pkgver.tar.gz
	rootfs-etc-NetworkManager-conf.d-00-sxmo.conf
	rootfs-etc-polkit-1-rules.d-00-sxmo.rules
	rootfs-etc-polkit-1-rules.d-50-org.freedesktop.NetworkManager.rules
	"

package() {
	mkdir -p "$pkgdir/etc/modules-load.d/"
	printf %b "snd-aloop" > "$pkgdir/etc/modules-load.d/sxmo.conf"
	mkdir -p "$pkgdir/etc/modprobe.d/"
	printf %b "options snd slots=,snd-aloop" > "$pkgdir/etc/modprobe.d/sxmo.conf"

	make -C "$builddir" DESTDIR="$pkgdir" install

	install -Dm644 "$srcdir/rootfs-etc-NetworkManager-conf.d-00-sxmo.conf" \
		"$pkgdir/etc/NetworkManager/conf.d/00-sxmo.conf"

	install -Dm644 "$srcdir/rootfs-etc-polkit-1-rules.d-00-sxmo.rules" \
		"$pkgdir/etc/polkit-1/rules.d/00-sxmo.rules"

	install -Dm644 "$srcdir/rootfs-etc-polkit-1-rules.d-50-org.freedesktop.NetworkManager.rules" \
		"$pkgdir/etc/polkit-1/rules.d/50-org.freedesktop.NetworkManager.rules"
}

common() {
	pkgdesc="$pkgdesc - common dependencies"
	depends="
		$pkgname=$pkgver-r$pkgrel
		$pkgname-audio
		$pkgname-wm

		adwaita-icon-theme
		alsa-utils
		brightnessctl
		callaudiod
		codemadness-frontends
		conky
		dnsmasq
		dunstify
		file
		font-dejavu
		font-dejavu-sans-mono-nerd
		geoclue
		inotify-tools
		linux-tools-iio
		lisgd
		mediainfo
		mmsd-tng
		mmsd-tng-tools
		mnc
		modemmanager
		mpv
		ncurses
		pnc
		polkit
		pulseaudio-utils
		superd
		tinydm
		tzdata
		upower
		v4l-utils
		vim
		vvmd
		xdg-user-dirs
		yt-dlp
		"

	# bonsai and sxmobar requires hare which doesnt work on all arches
	# refer to https://pkgs.alpinelinux.org/packages?name=hare&branch=edge&repo=&arch=&maintainer=
	# and add arches here as they become available. Bonsai should always be preferred
	# cause it fixes alot of issues. If bonsai is not installed, sxmo will fallback
	# to the old buggy shell script.
	case "$CARCH" in
	x86_64|aarch64|riscv64)
		depends="$depends bonsai sxmobar"
		;;
	esac

	mkdir -p "$subpkgdir"
}

x11() {
	pkgdesc="$pkgdesc - x11 dependencies"
	depends="
		$pkgname-common=$pkgver-r$pkgrel

		autocutsel
		dunst
		feh
		svkbd
		sxiv
		sxmo-dmenu
		sxmo-dwm
		sxmo-st
		unclutter-xfixes
		xcalib
		xclip
		xdotool
		xdpyinfo
		xinput
		xob
		xprintidle
		xprop
		xrandr
		xrdb
		xsel
		xset
		xsetroot
		xwininfo
		"

	mkdir -p "$subpkgdir"
}

wayland() {
	pkgdesc="$pkgdesc - wayland dependencies"
	depends="
		$pkgname-common=$pkgver-r$pkgrel

		bemenu
		foot
		grim
		mako
		slurp
		swaybg
		swayidle
		wl-clipboard
		wob
		wtype
		wvkbd
		xwayland
		"

	mkdir -p "$subpkgdir"
}

dwm() {
	pkgdesc="$pkgdesc - dwm dependencies"
	provides="$pkgname-wm"
	provider_priority="10"
	depends="
		$pkgname-x11=$pkgver-r$pkgrel

		sxmo-dwm
		"
	install="$subpkgname.post-install"

	mkdir -p "$subpkgdir"
}

sway() {
	pkgdesc="$pkgdesc - sway dependencies"
	provides="$pkgname-wm"
	provider_priority="20"
	depends="
		$pkgname-wayland=$pkgver-r$pkgrel

		sway
		seatd
		xdg-desktop-portal-gtk
		xdg-desktop-portal-wlr
		"
	install="$subpkgname.post-install"

	mkdir -p "$subpkgdir"
}

audio_pipewire() {
	provides="$pkgname-audio"
	provider_priority="20"
	depends="pipewire-pulse pipewire wireplumber pipewire-alsa"

	mkdir -p "$subpkgdir"
}

audio_pulseaudio() {
	provides="$pkgname-audio"
	provider_priority="10"
	depends="pulseaudio pulseaudio-alsa"

	mkdir -p "$subpkgdir"
}

audio_bluetooth() {
	mkdir -p "$subpkgdir"
}

audio_bluetooth_pipewire() {
	install_if="$pkgname-audio-bluetooth=$pkgver-r$pkgrel $pkgname-audio-pipewire=$pkgver-r$pkgrel"
	depends="
		bluez
		pipewire-spa-bluez
		"

	mkdir -p "$subpkgdir"
}

audio_bluetooth_pulseaudio() {
	install_if="$pkgname-audio-bluetooth=$pkgver-r$pkgrel $pkgname-audio-pulseaudio=$pkgver-r$pkgrel"
	depends="
		bluez
		pulseaudio-bluez
		!$pkgname-audio-pipewire
		"

	mkdir -p "$subpkgdir"
}


sha512sums="
2fa76e16424d1e9b8a3e2ac4c8f525cbbd296670c134591736d51f0afb8f8c0f944764a0f01eb7d0655800d932f7b4b4353448ae2dda73b7358d611ce13ad6cc  sxmo-utils-1.16.3.tar.gz
67a031f309a3232ac1e8abc3fedeaee912c035f9c81b4f709248895905a27ab5844ec92c65e55b79af3894450ba3883549d4004f11efebb47114d41f730e4a5f  rootfs-etc-NetworkManager-conf.d-00-sxmo.conf
32532ad5071588b80f43d539b3bd140e3f5b4100fca244be2ade5b8c697c76043ce5278ae7bf14762106aed2c9690f34c14e73031e5d86c11cc042c8f7245421  rootfs-etc-polkit-1-rules.d-00-sxmo.rules
7b34e861bc4ec57d970d2a7aacca36e3c3742a63ff995af1336f80b666e4a38c1035b35e129322e0d62dc75011bfaf495eb2bcae5392f2bf39f1d7b1400afcce  rootfs-etc-polkit-1-rules.d-50-org.freedesktop.NetworkManager.rules
"
